

$(function () {
    var data=0;
    getGoodLists(0);
    function getTitle(){
        //��̬��ȡ�����˵���ť
        $.ajax({
            url:"http://139.199.157.195:9090/api/getbaicaijiatitle",
            success: function (res) {
                var html = template("navlist",res);
                //console.log(res);
                $("#toggle").html(html);

                //������ť�仯��Ʒ�б�
                var ul=document.getElementById("toggle");
                var lis = ul.children;
                for(var i=0;i<lis.length;i++){
                    lis[i].index=i;
                    lis[0].className="current";
                    lis[i].onclick = function () {
                        for(var j=0;j<lis.length;j++){
                            lis[j].className = "";
                        }
                        this.className = "current";
                        var data = this.getAttribute("kknav"); 
                        //console.log(data);
                        getGoodLists(data);
                        //ȥ��֪ͨ��
                        if(data!=0){
                            var notice = document.getElementById("notice");
                            notice.style.display="none";
                        }
                    }
                }
            }
        })
    }
    getTitle();


    //������

    var newData = {result:[]};
    var total = 40;



    function getGoodLists(data){
        $.ajax({
            url:"http://139.199.157.195:9090/api/getbaicaijiaproduct?titleid="+data,
            success: function (res) {


                var html = template("tpl",res);
                $(".goodlist ul").html(html);
                getGoodList(data);
            }
        })
    }




    //��̬��ȡ��Ʒ�б�
    function getGoodList(data){
        $.ajax({
            url:"http://139.199.157.195:9090/api/getbaicaijiaproduct?titleid="+data,
            success: function (res) {
                //��������
                newData.result = res.result.concat(res.result);

                //newData.result = newData.result.concat(newData.result);
           //var html = template("tpl",res);
           //     //console.log(html);
           //     $(".goodlist ul").html(html);

                // console.log(newData);
                lodaded();
            }
        })
    }

    function  lodaded(){
        //�Ѿ����ص��б�����
        var loadLength = $(".load").length;
        // console.log(loadLength);
        //�������� ��ֹ��������
        if(loadLength > total){
            return;
        }
        var data1 = {result:[]};
        var length = 10;
        if(newData.result.length <= length) {
            length = newData.result.length;
        }
        // console.log(newData);
        for(var i = 0; i < length; i++) {
            data1.result.push(newData.result.shift());
        }
        //console.log(data1);
        var html = template("tpl",data1);
             //console.log(html);
             $(".goodlist ul").append(html);
    }

    /*�������ݵĿ���*/
    var flag = false;
    window.onscroll = function () {
        if(flag == true) {
            return;
        }else {
            flag = true;
            var height = $('.goodlist ul').height() - $(document.body).height();
            var b = height - $(document.body).scrollTop();
            //console.log(b);
            if(b < 50) {
                lodaded();
            }
            flag = false;
        }
    }
})





//��������������������
$(".search").on("click", function () {
    //$("#search").css("display","block");
    $("#search").toggleClass("close");
    $(".search").toggleClass("bgimg");
    //$(".search").css("background-image","url(./images/1.png)");
})


//�˵�����
// $(function () {
//     liSwipe();
// })

// function liSwipe(){
//     /*ͨ����װ��swipe������ʵ��*/
//     itcast.iScroll({
//         swipeDom:document.querySelector('#menu'),/*����������*/
//         swipeType:'x',/*�����ķ���*/
//         swipeDistance:200/*�����ľ���*/
//     });
// }

// 菜单滑动

var startX,endX,moveX;
var currentX = 0;//设置当前位置
var distanceX = 0;//设置当前的滑动距离
//设置滑动区间
var maxSwipe = 0+80;//设置最大的滑动距离
var minSwipe = $("#menu").width()-$("#toggle").width()-80;//设置最小的滑动距离
//设置位移区间 
var maxposition = 0;//最大的位移位置
var minposition = $("#menu").width()-$("#toggle").width();//最小的位移位置
//获取页面滑动目标元素
var ull = $("#toggle");
console.log(ull);
$("#menu").on("touchstart",function(e){
    startX = e.originalEvent.touches[0].clientX;
})
$("#menu").on("touchmove",function(e){
    moveX = e.originalEvent.touches[0].clientX;
    distanceX = moveX - startX;
    //判断条件 
    if((currentX+distanceX) < maxSwipe && (currentX+distanceX) > minSwipe){
        ull.css("transform","translateX("+(currentX+distanceX)+"px)");
        ull.css("transition", "none");
 }
})
$("#menu").on("touchend",function(e){
//    endX = e.originalEvent.changeTouches[0].clientX;
   currentX+=distanceX;
     //松开手的时候要弹回去
            if (currentX > maxposition) {
                currentX = maxposition;
                ull.css("transform", "translateX(" + currentX + "px)");
                ull.css("transition", "all 0.2s");
            } else if (currentX < minposition) {
                currentX = minposition;
                ull.css("transform", "translateX(" + currentX + "px)");
                ull.css("transition", "all 0.2s");
            }
            //数据还原清空
            // currentX=0;
            // startX=0;
            // distanceX=0;
})



